package com.sq.partner.manager.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sq.partner.manager.dao.FacadeAppApplicantDao;
import com.sq.partner.manager.dao.FacadeAppBeneficiaryDao;
import com.sq.partner.manager.dao.FacadeAppFormDao;
import com.sq.partner.manager.dao.FacadeAppInsuredDao;
import com.sq.partner.manager.dao.FacadeTempPolicyDao;
import com.sq.partner.manager.mybatis.model.FacadeTempPolicy;
import com.sq.partner.manager.service.FacadeAppBeneficiaryService;
import com.sq.partner.manager.service.FacadeAppFormService;
import com.sq.partner.manager.service.FacadeAppInsuredService;
import com.sq.partner.manager.service.FacadeTempPolicyService;
import com.sq.partner.manager.util.DateUtil;

@Service
public class FacadeTempPolicyServiceImpl implements FacadeTempPolicyService {

    @Resource
    FacadeTempPolicyDao tempPolicyDao;

    @Resource
    FacadeAppFormDao facadeAppFormDao;

    @Resource
    FacadeTempPolicyDao facadeTempPolicyDao;

    @Resource
    FacadeAppInsuredDao facadeAppInsuredDao;

    @Resource
    FacadeAppApplicantDao facadeAppApplicantDao;

    @Resource
    FacadeAppBeneficiaryDao facadeAppBeneficiaryDao;

    @Resource
    FacadeAppBeneficiaryService facadeAppBeneficiaryService;
    @Resource
    FacadeAppInsuredService facadeAppInsuredService;
    @Resource
    FacadeAppFormService facadeAppFormService;

    /**
     * 生成暂存单、维护暂存单表，查询最大的暂存单号是否存在，如果存在就+1,按照生成规则保存
     * 如果不存在，第一个暂存单号为“ZC20180000000001" ——2018是取当前年
     */
    @Override
    public FacadeTempPolicy saveFacadeTempPolicy(
            FacadeTempPolicy facadeTempPolicy) throws Exception {
        // 生成一个新的暂存单号
        String newStagNo = facadeTempPolicyDao.queryNewStagNo();
        facadeTempPolicy.setStagNo(newStagNo);
        facadeTempPolicy.setCreatTime(DateUtil.getNowTimestamp());
        facadeTempPolicy.setUpdateTime(DateUtil.getNowTimestamp());
        // 增加暂存单信息
        tempPolicyDao.insertFacadeTempPolicy(facadeTempPolicy);
        return facadeTempPolicy;
    }

    /**
     * 查找暂存单
     */
    @Override
    public FacadeTempPolicy findFacadeTempPolicy(
            FacadeTempPolicy facadeTempPolicy) throws Exception {

        return facadeTempPolicyDao.selectFacadeTempPolicy(facadeTempPolicy);
    }

    /**
     * 查询暂存单信息
     * 
     * @param facadeTempPolicy
     * @return
     * @throws Exception
     */
    @Override
    public FacadeTempPolicy getFacadeTempPolicy(String stagNo) throws Exception {
        return (FacadeTempPolicy) tempPolicyDao
                .queryFacadeTempPolicyById(stagNo);
    }
}
